Defining a Package

  1. PACKAGES:

Java provides a mechanism for partitioning the class name space into more manageable chunks. This mechanism is the package. The package is both a naming and a visibility control mechanism. You can define classes inside a package that are not accessible by code outside that package. You can also define class members that are only exposed to other members of the same package.

    1. Defining a package:

To create a package, include a package command as the first statement in a Java source file. Any classes declared within that file will belong to the specified package.

This is the general form of the package statement:
package pkg;

Here, pkg is the name of the package. For example, the following statement creates a package called MyPackage.
package MyPackage;
You can create a hierarchy of packages. To do so, simply separate each package name
from the one above it by use of a period. The general form:

package pkg1[.pkg2[.pkg3]];


JAVA Packages and Interfaces